package cn.gym.controller;

import cn.gym.pojo.Customer;
import cn.gym.pojo.Trainer;
import cn.gym.pojo.User;
import cn.gym.service.CustomerService;
import cn.gym.service.TrainerService;
import cn.gym.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private CustomerService customerService;
    @Resource
    private TrainerService trainerService;

    public void setTrainerService(TrainerService trainerService) {
        this.trainerService = trainerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    /**
     * 模糊查询用户信息
     * @return
     */
    @RequestMapping("/queryUserList")
    public String queryUserList(){
        return null;
    }

    /**
     * 注册会员用户
     * @return
     */
    @RequestMapping(value = "/registerCustomer",method = RequestMethod.POST)
    public String register(@RequestParam(value = "userCode",required = true)String userCode,
                           @RequestParam(value = "userPassword",required = true)String password,
                           @RequestParam(value = "custname",required = true)String custname,
                           @RequestParam(value = "email",required = false)String email,
                           @RequestParam(value = "phone",required = true)String phone,
                           @RequestParam(value = "address",required = false)String address,
                           @RequestParam(value = "totalmoney",required = false)String totalmoney
                           ){
        /**
         * 创建user对象，保存会员账号、密码、身份信息
         */
        User user=new User();
        user.setUsercode(userCode);
        user.setPassword(password);
        user.setRoleid(2);

        Boolean reg=userService.register(user);
        if(reg){//新用户添加成功
            /**
             * 查询
             */
            int uid=userService.selectByUserCode(userCode);
            /**
             * 创建customer会员用户对象，保存会员基本注册信息
             */
            Customer customer=new Customer();
            customer.setPhone(phone);
            customer.setCustname(custname);
            customer.setAddress(address);
            customer.setEmail(email);
            customer.setTotalmoney(totalmoney);
            customer.setUid(uid);

            boolean regCust=customerService.register(customer);
            if(regCust){//会员注册成功
                return "redirect:/user/queryUserList";
            }else {
                throw new RuntimeException("会员注册失败！");
            }
        }else {
            throw new RuntimeException("会员注册失败！");
        }
    }
    /**
     * 注册教练
     * @return
     */
    @RequestMapping(value = "/registerTrainer",method = RequestMethod.POST)
    public String register(@RequestParam(value = "userCode",required = true)String userCode,
                           @RequestParam(value = "password",required = true)String password,
                           @RequestParam(value = "trainername",required = true)String trainername,
                           @RequestParam(value = "description",required = false)String description
    ){
        /**
         * 创建user对象，保存会员账号、密码、身份信息
         */
        User user=new User();
        user.setUsercode(userCode);
        user.setPassword(password);
        user.setRoleid(1);

        Boolean reg=userService.register(user);
        if(reg){//新用户添加成功
            /**
             * 查询
             */
            int uid=userService.selectByUserCode(userCode);
            /**
             * 创建trainer会员用户对象，保存会员基本注册信息
             */
            Trainer trainer=new Trainer();
            trainer.setUserid(uid);
            trainer.setDescription(description);
            trainer.setTrainername(trainername);
            boolean regTrainer=trainerService.register(trainer);
            if(regTrainer){//教练注册成功
                return "redirect:/user/queryUserList";
            }else {
                throw new RuntimeException("教练注册失败！");
            }
        }else {
            throw new RuntimeException("教练注册失败！");
        }
    }
}
